package thread;

import java.util.Date;

/**
 * @author: feiwang_6
 * @create: 2019/10/24 8:12
 * @description:
 */
public class MyThread extends Thread {
    public static InheritableThreadLocal<Long> inheriLocal = new InheritableThreadLocal(){
        @Override
        protected Object initialValue() {
            long time = new Date().getTime();
            System.out.println(Thread.currentThread().getName() + " thread Initial " + time);
            return time;
        }
    };
    
    public static ThreadLocal<Long> local = ThreadLocal.withInitial(() -> {
        long time = new Date().getTime();
        System.out.println(Thread.currentThread().getName() + " thread Initial " + time);
        return time;
    });
    
    @Override
    public void run() {
        for (int i = 0; i < 5; i++){
            System.out.println("MyThread " + inheriLocal.get());
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void main(String[] args) {
        for(int i = 0; i < 5; i++){
            System.out.println("mainThread " + inheriLocal.get());
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        Thread t = new MyThread();
        t.start();
    }
}